package edu.nf.exam.filter;


import javax.servlet.*;
import java.io.IOException;



/**
 * @Author : LLH
 * @create 2021/5/31
 * 编写过滤器 -- 实现统一编码设置
 */
public class EncodingFilter implements Filter {
    private static String encode;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.encode = filterConfig.getInitParameter("encode");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if(encode!=null){
            servletRequest.setCharacterEncoding(encode);
            servletResponse.setCharacterEncoding(encode);
        }

        // 放行
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("统一编码过滤器--销毁");
    }


}
